package cn.tedu.ivos2.vehicle.service.Impl;

import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleSaveDTO;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos2.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos2.vehicle.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> queryVehicle(VehicleQuery vehicleQuery) {
        return vehicleMapper.query(vehicleQuery) ;
    }

    @Override
    public void save(VehicleSaveDTO vehicleSaveDTO) {
        Vehicle vehicle=new Vehicle();
        BeanUtils.copyProperties(vehicleSaveDTO,vehicle);
        vehicle.setUpdateTime(new Date());
        if (vehicle.getId()==null){
            vehicle.setStatus("1");
            vehicle.setCreateTime(new Date());
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.insert(vehicle);
        }else {
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        vehicleMapper.deleteById(id);
    }

    @Override
    public void unbind(Long id) {
        Vehicle vehicle=new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateUnbind(vehicle);
    }

    @Override
    public void bind( Long geoId,Long id) {
        Vehicle vehicle=new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceId(geoId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
